﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EBSystem.Data;
using EBSystem.ViewModels;

namespace EBSystem.Pages
{
    /// <summary>
    /// LoginPage.xaml 的交互逻辑
    /// </summary>
    public partial class LoginPage : UserControl
    {
        public LoginPage()
        {
            InitializeComponent();

            DataContext = new LoginViewModel();
        }

        private void loginPageRegisterButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.GetThisMainWindow()?.GotoRegisterPage();
        }

        private void loginPageLoginButton_Click(object sender, RoutedEventArgs e)
        {
            long accountID = ((LoginViewModel)DataContext).Login();
            if (accountID == -1)
            {
                MessageBox.Show("用户名不存在");
                return;
            }
            else if (accountID == -2)
            {
                MessageBox.Show("密码不正确");
                return;
            }
            else if (accountID == -3)
                return;

            loginPagePasswordBox.Password = "";
            if (UserData.accountType == "用户")
            {
                MainWindow.GetThisMainWindow()?.GotoUserSourcePage();
            }
            else if (UserData.accountType == "商家")
            {
                MainWindow.GetThisMainWindow()?.GotoBusinessSourcePage();
            }
        }

        private void loginPagePasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            ((LoginViewModel)DataContext).UserPasswordInput = loginPagePasswordBox.Password;
            ((LoginViewModel)DataContext).InputCheck();
        }
    }
}
